package com.yitao.cms.controller.overseasLogisticsInfo;

import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.skipper.base.json.JSONObject;
import com.yitao.cms.constant.UserErrorEnum;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.dto.overseasLogistics.OverseasLogisticsErrorDto;
import com.yitao.cms.dto.overseasLogistics.OverseasLogisticsInfoDto;
import com.yitao.cms.enums.OverseasLogisticsEnums;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.service.overseasLogisticsInfo.OverseasLogisticsErrorService;
import com.yitao.cms.service.overseasLogisticsInfo.OverseasLogisticsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: wwl
 * @Date: 2019/4/9 11:58
 * @Description:
 */
@RestController
@RequestMapping(value = "/cms/overseasLogistics", produces = {"application/json"})
@Api(description = "境外物流信息相关数据", tags = {"cms-overseasLogistics"})
public class OverseasLogisticsInfoController {
    @Autowired
    private OverseasLogisticsInfoService overseasLogisticsInfoService;
    @Autowired
    private OverseasLogisticsErrorService overseasLogisticsErrorService;

    @ApiOperation("境外物流信息相关数据导入")
    @RequestMapping(value = "/importDate", method = RequestMethod.POST)
    public DataOutput importDate(@ApiParam(value = "Excel文件", required = true)
                                 @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        //获取登录信息
        OperationUserCmsModel userSession = UserSession.getUserSession(request);
        if (userSession == null) {
            return new DataOutput(UserErrorEnum.NULL_LOGIN.getCode(), UserErrorEnum.NULL_LOGIN.getDesc());
        }
        return overseasLogisticsInfoService.dateImport(file, userSession);
    }

    @ApiOperation("境外物流信息相关数据查询")
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    public PagedOutput<OverseasLogisticsInfoDto> findProductList(
            @ApiParam(value = "境外快递单号") @RequestParam(value = "overseaExpressNo", required = false) String overseaExpressNo,
            @ApiParam(value = "订单来源") @RequestParam(value = "orderSource", required = false) String orderSource,
            @ApiParam(value = "内部订单号") @RequestParam(value = "customerRefNo", required = false) String customerRefNo,
            @ApiParam(value = "物流服务商") @RequestParam(value = "logisticsProviders", required = false) String logisticsProviders,
            @ApiParam(value = "买家姓名") @RequestParam(value = "buyerName", required = false) String buyerName,
            @ApiParam(value = "买家身份证号码") @RequestParam(value = "buyerIdCard", required = false) String buyerIdCard,
            @ApiParam(value = "入库预报状态") @RequestParam(value = "p2Status", required = false) Integer p2Status,
            @ApiParam(value = "发货预报状态") @RequestParam(value = "p4Status", required = false) Integer p4Status,
            @ApiParam(value = "电商平台下单开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "电商平台下单结束时间") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo") Integer pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize") Integer pageSize
    ) {
        PagedInput pagedInput = new PagedInput(pageSize, pageNo);
        return overseasLogisticsInfoService.findList(overseaExpressNo, orderSource, customerRefNo, logisticsProviders, buyerName, buyerIdCard, p2Status, p4Status, staDate, endDate, pagedInput);
    }

    @ApiOperation("境外物流详情信息相关数据导出")
    @RequestMapping(value = "/findProductListExport", method = RequestMethod.GET)
    public void findProductListExport(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "境外快递单号") @RequestParam(value = "overseaExpressNo", required = false) String overseaExpressNo,
            @ApiParam(value = "订单来源") @RequestParam(value = "orderSource", required = false) String orderSource,
            @ApiParam(value = "内部订单号") @RequestParam(value = "customerRefNo", required = false) String customerRefNo,
            @ApiParam(value = "物流服务商") @RequestParam(value = "logisticsProviders", required = false) String logisticsProviders,
            @ApiParam(value = "买家姓名") @RequestParam(value = "buyerName", required = false) String buyerName,
            @ApiParam(value = "买家身份证号码") @RequestParam(value = "buyerIdCard", required = false) String buyerIdCard,
            @ApiParam(value = "入库预报状态") @RequestParam(value = "p2Status", required = false) Integer p2Status,
            @ApiParam(value = "发货预报状态") @RequestParam(value = "p4Status", required = false) Integer p4Status,
            @ApiParam(value = "电商平台下单开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "电商平台下单结束时间") @RequestParam(value = "endDate", required = false) String endDate) {
        overseasLogisticsInfoService.findProductListExport(request, response, overseaExpressNo,orderSource, customerRefNo, logisticsProviders, buyerName, buyerIdCard, p2Status, p4Status, staDate, endDate);
    }
    @ApiOperation("境外物流主信息相关数据导出")
    @RequestMapping(value = "/findListExport", method = RequestMethod.GET)
    public void findListExport(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "境外快递单号") @RequestParam(value = "overseaExpressNo", required = false) String overseaExpressNo,
            @ApiParam(value = "订单来源") @RequestParam(value = "orderSource", required = false) String orderSource,
            @ApiParam(value = "内部订单号") @RequestParam(value = "customerRefNo", required = false) String customerRefNo,
            @ApiParam(value = "物流服务商") @RequestParam(value = "logisticsProviders", required = false) String logisticsProviders,
            @ApiParam(value = "买家姓名") @RequestParam(value = "buyerName", required = false) String buyerName,
            @ApiParam(value = "买家身份证号码") @RequestParam(value = "buyerIdCard", required = false) String buyerIdCard,
            @ApiParam(value = "入库预报状态") @RequestParam(value = "p2Status", required = false) Integer p2Status,
            @ApiParam(value = "发货预报状态") @RequestParam(value = "p4Status", required = false) Integer p4Status,
            @ApiParam(value = "电商平台下单开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "电商平台下单结束时间") @RequestParam(value = "endDate", required = false) String endDate) {
        overseasLogisticsInfoService.findListExport(request, response, overseaExpressNo,orderSource, customerRefNo, logisticsProviders, buyerName, buyerIdCard, p2Status, p4Status, staDate, endDate);
    }

    @ApiOperation("上传身份证图片信息")
    @RequestMapping(value = "/updateIdImage", method = RequestMethod.POST)
    public DataOutput updateIdImage(
            HttpServletRequest request,
            @ApiParam(value = "id") @RequestParam(value = "id") String id,
            @ApiParam(value = "买家(收货人)身份证照片正面") @RequestParam(value = "buyerIdcardFrontend") String buyerIdcardFrontend,
            @ApiParam(value = "买家(收货人)身份证照片背面") @RequestParam(value = "buyerIdcardBackend") String buyerIdcardBackend) {
        return overseasLogisticsInfoService.updateIdImage(request, id, buyerIdcardFrontend, buyerIdcardBackend);

    }

    @ApiOperation("详情")
    @RequestMapping(value = "/findInfById", method = RequestMethod.POST)
    public DataOutput findInfById(
            @ApiParam(value = "id") @RequestParam(value = "id") String id
    ) {
        return overseasLogisticsInfoService.findInfById(id);

    }

    @ApiOperation("入库预报状态枚举列表")
    @RequestMapping(value = "/p2StatusList", method = RequestMethod.POST)
    public DataOutput p2StatusList() {
        return new DataOutput(OverseasLogisticsEnums.p2Status.getP2StatusEnum());
    }

    @ApiOperation("入发货预报状态枚举列表")
    @RequestMapping(value = "/p4StatusList", method = RequestMethod.POST)
    public DataOutput p4StatusList() {
        return new DataOutput(OverseasLogisticsEnums.p4Status.getP4StatusEnum());
    }

    @ApiOperation("httpShipments")
    @RequestMapping(value = "/httpShipments", method = RequestMethod.POST)
    public void httpShipments() {
        overseasLogisticsInfoService.httpShipments();
    }
    @ApiOperation("物流服务商枚举")
    @RequestMapping(value = "/logisticsProvidersEnum", method = RequestMethod.POST)
    public DataOutput logisticsProvidersEnum() {
        return new DataOutput(OverseasLogisticsEnums.logisticsProvidersEnum.getLogisticsProvidersEnum());
    }

    /****************************接收入库状态推送*************************************/
    @ApiOperation("接收入库状态推送-P2")
    @RequestMapping(value = "/warehouse/Callback", method = RequestMethod.POST)
    public JSONObject warehouseCallback(HttpServletRequest request, HttpServletResponse response){
        return overseasLogisticsInfoService.warehouseCallback(request);
    }

    @ApiOperation("接受转运状态推送-P4")
    @RequestMapping(value = "/transferStatus/Callback", method = RequestMethod.POST)
    public JSONObject transferStatusCallback(HttpServletRequest request, HttpServletResponse response){
        return overseasLogisticsInfoService.transferStatusCallback(request);
    }



//    @ApiOperation("测试接口")
//    @RequestMapping(value = "/test", method = RequestMethod.POST)
//    public DataOutput test(@ApiParam(value = "测试参数") @RequestParam(value = "testParam") String testParam){
//        WarehouseCallbackVo warehouseCallbackVo = new WarehouseCallbackVo();
//        warehouseCallbackVo.setOversea_express_no("1Z10249503536459151");
//        return overseasLogisticsInfoService.updateOverseasLogistics(warehouseCallbackVo);
//    }

    @ApiOperation("createOrder")
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public void createOrder() {
        overseasLogisticsInfoService.createOrder();
    }

    @ApiOperation("根据id调用p1接口")
    @RequestMapping(value = "/httpShipmentsById", method = RequestMethod.POST)
    public DataOutput httpShipmentsById(
            @ApiParam(value = "id") @RequestParam(value = "id") String id
    ) {
        return overseasLogisticsInfoService.httpShipmentsById(id);
    }
    @ApiOperation("根据id调用p3接口")
    @RequestMapping(value = "/createOrderById", method = RequestMethod.POST)
    public DataOutput createOrderById(
            @ApiParam(value = "id") @RequestParam(value = "id") String id
    ) {
        return overseasLogisticsInfoService.createOrderById(id);
    }

    @ApiOperation("异常回调信息列表")
    @RequestMapping(value = "/overseasLogisticsError", method = RequestMethod.POST)
    public PagedOutput<OverseasLogisticsErrorDto> overseasLogisticsError(@ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo") Integer pageNo,
                                                                         @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize") Integer pageSize) {
        return overseasLogisticsErrorService.findOverseasLogisticsError(pageNo, pageSize);
    }

    @ApiOperation("手动推送入库预报")
    @RequestMapping(value = "/artificialPush", method = RequestMethod.POST)
    public DataOutput artificialPush(@ApiParam(value = "异常信息id", required = true) @RequestParam("id") Long id){
        return overseasLogisticsErrorService.artificialPush(id);
    }


}
